/**
 * 初始化整体流量图标
 */
define(function(){
    var ec=require('echarts');
    require('echarts/chart/scatter');
    var zrnumber = require('echarts/util/number');

    var myChart1 = ec.init(document.getElementById('customerCosttransfer')); 

   var option = {
    title : {
        text: '转换散点图',
        subtext: '可区域选择'
    },
    tooltip : {
        trigger: 'axis',
        showDelay : 0,
        formatter : function (params) {
            if (params.value.length > 1) {
                return params.seriesName + ' :<br/>'
                   + '用户ID: '+params.value[2]+ '<br/>'
                   + params.value[0] + '元 ' 
                   + params.value[1] + '元 ';
            }
            else {
                return params.seriesName + ' :<br/>'
                   + '用户ID: '+params.value[2]+ '<br/>'
                   + params.name + ' : '
                   + params.value + '元 ';
            }
        },  
        axisPointer:{
            show: true,
            type : 'cross',
            lineStyle: {
                type : 'dashed',
                width : 1
            }
        }
    },
    toolbox: {
        show : true,
        feature : {
            mark : {show: true},
            dataZoom : {show: true},
            dataView : {show: true, readOnly: false},
            restore : {show: true},
            saveAsImage : {show: true}
        }
    },
    legend: {
        data :['转精准','转置顶','流失']
    },
    xAxis : [
        {
            type : 'value',
            name : '消费',
            scale:true,
            axisLabel : {
                formatter: '{value} 元'
            }
        }
    ],
    yAxis : [
        {
            type : 'value',
            name : '余额',
            scale:true,
            axisLabel : {
                formatter: '{value} 元'
            }
        }
    ],
    series : [ 
              {
                name: '总人数',
                type: 'scatter',
                data:[]
             }]
       
};
   var nameMap = {"tojingzhun":"转精准","tozhiding":"转置顶","flow":"流失"};
    function show(){
        //myChart1.setOption(option,true);
        var businessName=cate;
        var dateGrp = $('#customerCosttransferOption').attr('dateGrp');
        var datetype = $('#customerCosttransferOption').attr('statType');
        var selDate=$('#time_input_CosttransferPro').val();
        var CusbalanceType= $('#customerCosttransferOption').attr('Costtransfertype');
        myChart1.showLoading({ text: '正在努力的读取数据中...' });
            $.ajax({
                    url: '/dashboard/income/ajax/customerCosttransferData',
                    type: 'post',
                    async: true,
                    data:{"dateGroup":dateGrp,"customerType":CusbalanceType,"statType":datetype,"businessName":businessName,"selectDate":selDate},
                    dataType: 'json',
                    success: function(data) {
                     var resultList = data.dataList;
                     var i = 0;
                     var series_local = [];
                    if(selDate=='')
                     {
                        $('#time_input_CosttransferPro').val(data['maxdate']);
                     }    
                   $.each(nameMap,function(colName,name){   
                               series_local[i++] = {
                                name:name,
                                type:'scatter',
                                'data': data[colName]
                                };
                             });
                       option.series = series_local;
                       myChart1.setOption(option,true);
                       myChart1.hideLoading();

                        },
                    error : function() {
                        myChart1.hideLoading();
                    }
                    });
    }
    var EC={
        show:show,
        resize:function(){myChart1.resize();}
    };
     function formatShowDate(yyyymmdd){
        var dateGrp = $('#customerCosttransferOption').attr('dateGrp');
        if('stat_date'==dateGrp){
            return yyyymmdd;
        }else if('week_num'==dateGrp){
            return yyyymmdd;
        }else if('month_num'==dateGrp){
            return yyyymmdd.substr(0,4)+"年"+yyyymmdd.substr(4,2)+"月";
        }
    }

    function isDay(){
        var dateGrp = $('#customerCosttransferOption').attr('dateGrp');
        if('stat_date'==dateGrp){
            return true;
        }
        return false;
    }

        function isWeek(){
         var dateGrp = $('#customerCosttransferOption').attr('dateGrp');
        if('week_begin'==dateGrp){
            return true;
        }
        return false;
    }

    function isMonth(){
        var dateGrp = $('#customerCosttransferOption').attr('dateGrp');
        if('month_num'==dateGrp){
            return true;
        }
        return false;
    }
    //绑定change事件
    $("#Costtransfer_product").change(function(){
       changeIndexForCustransfer(this);   
        EC.show();
    });
     //绑定书刷新按钮点击事件
    $("#refreshCosttransfer_btn").click(function(){  
        EC.show();
    });
    $(":button.btnCustomerCosttransferPro[dateGrp]").click(function(){
        changeDateGrp(this);
       /* $('#time_checkbox_CosttransferPro').attr('checked',false);
        $('#time_input_CosttransferPro').val('');
        $('#time_input_CosttransferPro').attr('disabled','');
        if($(this).attr('dateGrp')=='week_num'){
            $('#time_input_CosttransferPro').parent().hide();
        }else{
            $('#time_input_CosttransferPro').parent().show()
        }*/
        
        EC.show();
    });
    //时间勾选框事件
    $('#time_checkbox_CosttransferPro').change(function(e){
        var input=$('#time_input_CosttransferPro');
        if(!input)return;
        if($(this).is(":checked")){
            input.removeAttr('disabled');
            input.attr('placeholder','选择时间'); 
            datepicker_op.format='yyyy-mm-dd';              
           /* EC.hideDataZoom();*/
           input.datepicker(datepicker_op);
           input.datepicker('setStartDate',new Date(getDate('2014/01/01')));
           var enddate=new Date();
           input.datepicker('setEndDate',new Date(getDate(enddate.toLocaleDateString())));
            /*setDatePicker(EC, $('#customerFunnelOption').attr('dateGrp'),input);*/
        }else{
            input.val('');
            input.attr('disabled','');
            input.attr('placeholder','选择时间');               
        }
    });
    /*//时间选择事件
    $('#time_input_FunnelPro').datepicker().on('changeDate',function(e){
        EC.addCompare_1(e.format());
        EC.addCompare_2(e.format());
    });*/

    return EC;
});

